package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Member;
import com.powernode.model.Result;
import com.powernode.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: Tong Ziyu
 * @Date: 2025/4/26 22:47
 */
@RestController
@RequestMapping("admin/user")
@Slf4j
@Api(tags = "会员管理")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 会员分页查询
     * @return
     */
    @ApiOperation("会员分页查询")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:user:page')")
    public Result<Page<Member>> loadMemberPage(
            @RequestParam(value = "current")Long current,
            @RequestParam(value = "size")Long size,
            @RequestParam(value = "nickName",required = false) String nickName,
            @RequestParam(value = "status", required = false) Integer status

    ){

        Page page = new Page<Member>(current,size);

        page = memberService.page(page,
                new LambdaQueryWrapper<Member>()
                        .like(nickName != null, Member::getNickName, nickName)
                        .eq(status != null, Member::getStatus, status)
                        .orderByDesc(Member::getCreateTime)
                );

        return Result.success(page);
    }
}
